package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.FramOrderStaticEntity;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/3.
 */

public class FramOrderStaticAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<FramOrderStaticEntity.DataBean> dataBeanList;
    private String orderitem;
    private Handler handler = new Handler();
    public FarmCallBack callBack;

    public void setCallBack(FarmCallBack callBack) {
        this.callBack = callBack;
    }

    public FramOrderStaticAdapter(Context context, List<FramOrderStaticEntity.DataBean> dataBeanList, String orderitem) {
        this.context = context;
        this.orderitem = orderitem;
        this.dataBeanList = dataBeanList;
    }

    public void refreshData(List<FramOrderStaticEntity.DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
        notifyDataSetChanged();
    }

//    public List<FramOrderStaticEntity.DataBean> getData() {
//        return dataBeanList;
//    }

    public List<FramOrderStaticEntity.DataBean> getData() {
        return dataBeanList;
    }

    @Override
    public int getCount() {
        return dataBeanList != null ? dataBeanList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FramOrderStaticViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fram_order_static_item, parent, false);
            holder = new FramOrderStaticViewHolder();
            holder.fram_order_static_item_hour = (TextView) convertView.findViewById(R.id.fram_order_static_item_hour);
            holder.fram_order_static_item_shop_name = (TextView) convertView.findViewById(R.id.fram_order_static_item_shop_name);
            holder.fram_order_static_item_price = (TextView) convertView.findViewById(R.id.fram_order_static_item_price);
            holder.fram_order_static_item_shop_num = (TextView) convertView.findViewById(R.id.fram_order_static_item_shop_num);
            holder.fram_order_static_item_phone = (TextView) convertView.findViewById(R.id.fram_order_static_item_phone);
            holder.fram_order_static_item_sure_in = (TextView) convertView.findViewById(R.id.fram_order_static_item_sure_in);
            holder.fram_order_static_item_picture = (ImageView) convertView.findViewById(R.id.fram_order_static_item_picture);
            holder.fram_order_static_item_ordertype = (TextView) convertView.findViewById(R.id.fram_order_static_item_ordertype);
            convertView.setTag(holder);
        } else {
            holder = (FramOrderStaticViewHolder) convertView.getTag();
        }
        holder.fram_order_static_item_shop_name.setText(dataBeanList.get(position).getName());
        holder.fram_order_static_item_hour.setText(dataBeanList.get(position).getTime());
        String phone = dataBeanList.get(position).getPhone();
        String subString = phone.substring(3, 7);
        String replace = phone.replace(subString, "****");
        holder.fram_order_static_item_phone.setText(replace);
        holder.fram_order_static_item_price.setText("￥" + dataBeanList.get(position).getPrice());
        holder.fram_order_static_item_shop_num.setText("×" + dataBeanList.get(position).getBuyNum());
        Picasso.with(context).load(dataBeanList.get(position).getImg()).into(holder.fram_order_static_item_picture);
        DecidePiaoType(holder, dataBeanList.get(position).getOrderType());
        holder.fram_order_static_item_sure_in.setTag(position);
        if (orderitem.equals("待确认")) {
            holder.fram_order_static_item_sure_in.setText("确认入园");
            holder.fram_order_static_item_sure_in.setOnClickListener(this);
        } else if (orderitem.equals("全部")) {
            if (dataBeanList.get(position).getStatus().equals("5")) {
                //已完成
                holder.fram_order_static_item_sure_in.setText("已确认");
            } else if (dataBeanList.get(position).getStatus().equals("3")) {
                //待确认
                holder.fram_order_static_item_sure_in.setText("确认入园");
                holder.fram_order_static_item_sure_in.setOnClickListener(this);
            } else if (dataBeanList.get(position).getStatus().equals("7")) {
                holder.fram_order_static_item_sure_in.setText("确认入园");
                holder.fram_order_static_item_sure_in.setOnClickListener(this);
            }
        } else if (orderitem.equals("已完成")) {
            holder.fram_order_static_item_sure_in.setText("已确认");
        }
        return convertView;
    }

    private void DecidePiaoType(FramOrderStaticViewHolder holder, String type) {
        if (type.equals("1")) {
            holder.fram_order_static_item_ordertype.setText("成人票");
        } else if (type.equals("2")) {
            holder.fram_order_static_item_ordertype.setText("学生票");
        } else if (type.equals("3")) {
            holder.fram_order_static_item_ordertype.setText("共享票");
        }
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        switch (v.getId()) {
            case R.id.fram_order_static_item_sure_in:
//                if (orderitem.equals("全部")) {
                if (dataBeanList.get(position).getStatus().equals("3")) {
                    Toast.makeText(context, "请等待用户确认入园", Toast.LENGTH_SHORT).show();
                } else if (dataBeanList.get(position).getStatus().equals("7")) {
                    ConfirmReceipt(position, "4");
                }
//                } else if (orderitem.equals("已确认")) {
//
//                }

                break;
        }
    }

    private void ConfirmReceipt(final int position, String type) {
        ShopHttpConfig.ConfirmReceipt(context, type, dataBeanList.get(position).getOrderid(), new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {
                Log.e("确认收货", "收货失败");
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }

            @Override
            public void suseff(Call call, Response response, Object o) {
                Log.e("确认入园", "收货成功");
                callBack.callBackListener();
                Intent intent = new Intent();
                intent.setAction("sure");
                context.sendBroadcast(intent);
            }
        }, BaseEntity.class);
    }

    public interface FarmCallBack {
        void callBackListener();
    }

    class FramOrderStaticViewHolder {
        TextView fram_order_static_item_hour;
        ImageView fram_order_static_item_picture;
        TextView fram_order_static_item_shop_name, fram_order_static_item_price, fram_order_static_item_shop_num, fram_order_static_item_phone;
        TextView fram_order_static_item_sure_in;
        TextView fram_order_static_item_ordertype;
    }
}
